var app = angular.module('qzApp', []);
var keyTip="请输入待检索的关键字";
	
app.controller('qzAppController', function($scope, $http, AppService) {	
	$scope.load=function(isRest){
		$scope.sel=$scope.sel?$scope.sel:{};
		var pageNum=$scope.pageInfo?isRest==false?$scope.pageInfo.pageNum:1:1;
		var pageSize=$scope.pageInfo?$scope.pageInfo.pageSize:10;
		$scope.sel.pageNum=pageNum;
		$scope.sel.pageSize=pageSize;
		var param={};  
        for(var k in $scope.sel){ 
        	param[k] = $scope.sel[k];  
        	if(k=="key"&&$scope.sel[k]==keyTip){
        		delete param[k];
        	}
        } 
		
		AppService.list(param).success(function (response) {
			$scope.pageInfo=response.data;
			$scope.pageInfo.pageLinks=getPageLinks(response.data.pageNum,response.data.pages);
		});	
	}	
	$scope.load();
    $scope=pagination.init($scope);
    
    $scope.qzAppCreator=function(){
    	redirectUrl("manage/apps/","add.jsp");
    } 
    
    $scope.refreshData=function(){    	  	
    	$scope.load();
    }
    
    $scope.lock=function(id){
    	AppService.lock(id).success(function (response) {
    		$scope.load();
    	});
    }
    
    $scope.qzAppDel=function(id){
    	$scope.delItem={id:id};
    	$('#box-del').modal({
    		show: true
    	});
    	
    }
    $scope.del=function(id){
     
    	AppService.del(id).success(function (response) {
    		$('#box-del').modal('hide');
    		//$scope.load();
    		redirectUrl("manage/apps/","list.jsp");
    	});
    }
    if(inputPlaceholder()){
    	$scope.sel.key=keyTip;
    }

});

app.factory('AppService', function ($http) {
	var list = function (param) {	 
        return $http({
            params: param,
            url: timestamp('qz/apps/getByPage')
        });
    };
    var del = function (id){
    	return $http.put('qz/apps/del/'+id);
    };
    var lock = function (id){
    	return $http.put('qz/apps/lock/'+id);
    }
	return {
		list:function(param){
			return list(param);
		},
		del:function(id){
			return del(id);
		},
		lock:function(id){
			return lock(id);
		}
	}
});
